package com.someok.utils.idgen;

/**
 * Simple synchronized sequence id generator. It generates long numbers in a
 * sequence. When counter reaches max long, it will be set back to zero.
 * 
 * <p><a href="SimpleIdGenerator.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 41 $ $Date: 2006-06-27 23:17:14 +0800 (星期二, 27 六月 2006) $
 */
public final class SimpleIdGenerator {

	private volatile long id = 0;

	/**
	 * Constructor for ID generator. It must to take the starting number for
	 * generator. Starting number is first availiable id.
	 *
	 * @param v      starting number
	 */
	public SimpleIdGenerator(long v) {
		id = v;
	}

	/**
	 * Returns the next id value. Thread-safe.
	 *
	 * @return next id value
	 */
	public synchronized final long next() {
		if (++id <= 0) {
			id = 0;
		}
		return id;
	}
}
